import requests
import json
import datetime

filed = {
    '帐号开通申请': ['account', 0],
    '内置审批服务': ['approval', 0],
    '事件管理': ['events', 0],
    'cmdb数据异常请求': ['cmdb', 0],
    'Zabbix监控告警事件处理': ['zabbix', 0],
    '宝汽销桌面问题': ['desktop', 0],
    'DMS系统问题': ['dms', 0],
    '005-CPU使用率告警转事件工单': ['cpu', 0]
}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36',
}

url = 'http://paas.bkpaas.baonengmotor.com/login/'
cont = requests.get(url=url, headers=headers)
csrf = cont.content[958:990].decode()

data = {
    'csrfmiddlewaretoken': csrf,
    'username': 'admin',
    'password': 'J5GwtcfEmkXc',
    'next': '',
    'app_id': ''
}

headers['Cookie'] = 'bklogin_csrftoken=%s' % csrf

session = requests.session()
# print(data)
login_ret = session.post(url=url+'?c_url=http://appdev.paas.bkpaas.baonengmotor.com:10010/', data=data, headers=headers)
today = datetime.datetime.now()
print(requests.utils.dict_from_cookiejar(login_ret.cookies))
yestoday = (datetime.datetime.now()-datetime.timedelta(days=1)).strftime("%Y-%m-%d")
params = {
    'is_periodic': True,
    'create_at__lte': yestoday + " 23:59:59",
    'create_at__gte': yestoday + " 00:00:00"
}


ret = session.get('http://appdev.paas.bkpaas.baonengmotor.com:10010/itsm/graph/data/', params=params)
print(ret)
data = json.loads(ret.content)['data']['items']
for item in data:
    if item['service_name'] in filed:
        filed[item['service_name']][1] += 1
for k, v in filed.values():
    exec(k+"=%d"%v)
print(account, approval, events, cmdb, zabbix, desktop, dms, cpu)